package com.neu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.mapper.UserMapper;
import com.neu.po.User;
import com.neu.po.UserExample;
import com.neu.po.UserExample.Criteria;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Override
	public User login(String username, String password) {
		UserExample example = new UserExample();
		
		example.or().andUsernameEqualTo(username)
		.andPasswordEqualTo(password);
		
		if(userMapper.selectByExample(example).size() > 0 ) {
			return userMapper.selectByExample(example).get(0);//查到第一个集合
		}else {
			return null;
		}
	}

	@Override
	public int insert(User user) {
		return userMapper.insert(user);
	}

	@Override
	public int update(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(user);
	}

	@Override
	public PageInfo<User> getPaged(int pageNum, int pageSize, String keyword) {
		UserExample example = new UserExample();
		
		Criteria c1 = example.createCriteria();
		
		c1.andUsernameLike("%"+keyword+"%");
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<User> list = userMapper.selectByExample(example);
		
		PageInfo<User> pageInfo = new PageInfo<>(list);
		
		return pageInfo;
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(id);
	}

}
